/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.menumanager.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import org.hibernate.annotations.GenericGenerator;

import cn.com.ksplatform.base.BaseModel;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButtonGroup;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;

/**
 * @ClassName:PlatMenu <br/>
 * @deprecated:TODO. <br/>
 * @Date: 2015年5月21日 下午4:13:23 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Entity
@Table(name = "t_sys_menu")
public class PlatMenu extends BaseModel implements Serializable,Comparable<PlatMenu>{

	@Id
	@GenericGenerator(name = "systemUUID", strategy = "uuid")
	@GeneratedValue(generator = "systemUUID")
	@Column(name = "menuId")
	private String menuId;
	private String menuName;
	private String url;
	private String iconCls;
	private String enabled;
	private Integer menuSort;
	
	@Transient
	public String parent_id;
	
	@ManyToOne(cascade = { CascadeType.PERSIST })
	@JoinColumn(name = "parent_id")
	@JsonIgnore
	private PlatMenu parent_menu;
	
	@OneToMany(mappedBy = "parent_menu", cascade = { CascadeType.ALL },fetch=FetchType.LAZY)
	@OrderBy("menuSort")
	@JsonIgnore
	private Set<PlatMenu> children = new HashSet<PlatMenu>(0);

	@OneToMany(mappedBy = "menu")
	@JsonIgnore
	private Set<PlatButtonGroup> btnGrp = new HashSet<PlatButtonGroup>(0);

	@OneToMany(mappedBy = "menu", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JsonManagedReference
	@JsonIgnore
	private Set<PlatResource> resources = new HashSet<PlatResource>(0);

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "t_sys_menu_role", joinColumns = @JoinColumn(name = "menuId"), inverseJoinColumns = @JoinColumn(name = "roleId"))
	private Set<PlatRole> roles = new HashSet<PlatRole>(0);

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getEnabled() {
		return enabled;
	}

	public void setEnabled(String enabled) {
		this.enabled = enabled;
	}

	public Set<PlatRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<PlatRole> roles) {
		this.roles = roles;
	}

	public Set<PlatResource> getResources() {
		return resources;
	}

	public void setResources(Set<PlatResource> resources) {
		this.resources = resources;
	}

	public PlatMenu getParent_menu() {
		return parent_menu;
	}

	public void setParent_menu(PlatMenu parent_menu) {
		this.parent_menu = parent_menu;
	}

	public Set<PlatMenu> getChildren() {
		return children;
	}

	public void setChildren(Set<PlatMenu> children) {
		this.children = children;
	}

	public Integer getMenuSort() {
		return menuSort;
	}

	public void setMenuSort(Integer menuSort) {
		this.menuSort = menuSort;
	}

	public PlatMenu(String menuId, String menuName, String url, String iconCls,
			String enabled, Integer menuSort) {
		super();
		this.menuId = menuId;
		this.menuName = menuName;
		this.url = url;
		this.iconCls = iconCls;
		this.enabled = enabled;
		this.menuSort = menuSort;
	}

	public PlatMenu() {
	}

	public Set<PlatButtonGroup> getBtnGrp() {
		return btnGrp;
	}

	public void setBtnGrp(Set<PlatButtonGroup> btnGrp) {
		this.btnGrp = btnGrp;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((menuId == null) ? 0 : menuId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PlatMenu other = (PlatMenu) obj;
		if (menuId == null) {
			if (other.menuId != null)
				return false;
		} else if (!menuId.equals(other.menuId))
			return false;
		return true;
	}

	@Override
	public int compareTo(PlatMenu arg0) {
		return this.getMenuSort().compareTo(arg0.getMenuSort());
	}
}
